package io.light.frame.dal.mybatis.generator.core.listener;

import io.light.frame.dal.mybatis.generator.core.context.GenResult;
import io.light.frame.dal.mybatis.generator.core.domain.Generation;
import io.light.frame.dal.mybatis.generator.core.domain.clazz.Clazz;
import io.light.frame.dal.mybatis.generator.core.domain.mapper.Mapper;
import io.light.frame.dal.mybatis.generator.core.domain.mapper.MapperFunc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * Logging generation listener
 *
 * @author Ivan
 * @version 1.0.0
 * @date 2021-06-17 13:42
 */
@Slf4j
@Component
public class LoggingGenerationListener implements GenerationListener {

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }

    /**
     * On generation start
     *
     * @param generation {@link Generation}
     */
    @Override
    public void onStart(Generation generation) {
        log(AnsiOutput.toString(AnsiColor.GREEN, ">> ") + "Generation start, detail:{}", generation);
    }

    /**
     * On metadata mapped
     *
     * @param mapper {@link Mapper}
     * @see io.light.frame.dal.mybatis.generator.core.meta.entity.Table
     * @see io.light.frame.dal.mybatis.generator.core.meta.entity.TableColumn
     */
    @Override
    public void onMetadataMapped(Mapper mapper) {
        log("Table '{}' metadata mapped ready", mapper.getTable().getName());
    }

    /**
     * On entity definition Ready
     *
     * @param entityClazz {@link Clazz}
     */
    @Override
    public void onEntityReady(Clazz entityClazz) {
        log("Entity '{}' definition ready", entityClazz.getName());
    }

    /**
     * On Mapper function definition Ready
     *
     * @param mapperFunc {@link MapperFunc}
     * @param content {@link MapperFunc.Content}
     */
    @Override
    public void onMapperFuncReady(MapperFunc mapperFunc, MapperFunc.Content content) {
        log("Mapper {} function '{}' definition ready", mapperFunc.getType(), mapperFunc.getId());
    }

    /**
     * On {@link Mapper} definition Ready
     *
     * @param mapper {@link Mapper}
     * @param daoClazz {@link Clazz}
     */
    @Override
    public void onMapperReady(Mapper mapper, Clazz daoClazz) {
        log("Dao '{}' definition ready", daoClazz.getName());
        log("Mapper completed, generating related files...", daoClazz.getName());
    }

    /**
     * On generation success
     *
     * @param result {@link GenResult}
     */
    @Override
    public void onSuccess(GenResult result) {
        GenResult.Detail detail = result.getDetail();
        log("Generated SUCCESS");
        log("Entity file: {}", detail.getEntityFile().getAbsolutePath());
        log("Dao file: {}", detail.getDaoFile().getAbsolutePath());
        log("Mapper xml: {}", detail.getMapperXml().getAbsolutePath());
    }

    /**
     * On generation error
     *
     * @param error {@link Exception}
     */
    @Override
    public void onError(Exception error) {

    }

    /**
     * On generation complete
     *
     * @param result {@link GenResult}
     */
    @Override
    public void onComplete(GenResult result) {

    }

    private void log(String msg, Object... args) {
        if (log.isInfoEnabled()) {
            log.info(msg, args);
        }
    }
}
